package cn.itcast.p2p20.fragments;


import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.adapter.AllProductListAdapter;
import cn.itcast.p2p20.bean.AllProductData;
import cn.itcast.p2p20.bean.AllProductDataBean;
import cn.itcast.p2p20.bean.request.AllProductRequest;
import cn.itcast.p2p20.bean.response.AllProductResponse;
import cn.itcast.p2p20.net.BaseRequest;
import cn.itcast.p2p20.net.SuccessResultCallback;
import cn.itcast.p2p20.utils.Util;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllProductListFragment extends BaseFragment implements SuccessResultCallback<AllProductResponse> {

    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;

    @Override
    protected void initData() {

        sendRequest(new AllProductRequest(), AllProductResponse.class, this);
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);

        //recycleView设置分割线
        recyclerview.addItemDecoration(new RecyclerView.ItemDecoration() {

            //设置条目之间的间隙
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                //条目距离同步的间隙
                if (parent.indexOfChild(view) != 0) {

                    outRect.top = (int) Util.dipToPx(mContext, 5);
                }

            }
        });
    }

    @Override
    protected String getTitle() {
        return null;
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_all_product_list;
    }

    @Override
    public void onSuccessResult(BaseRequest request, AllProductResponse response) {

        AllProductData allProductData = response.data;
        Log.d("AllProductListFragment", "allProductData:" + allProductData);

        List<AllProductDataBean> finances = allProductData.getFinance();
        List<AllProductDataBean> funds = allProductData.getFund();
        List<AllProductDataBean> invests = allProductData.getInvest();

        List<AllProductDataBean> allProductDataBeenList = new ArrayList<>();
        for (AllProductDataBean finance : finances) {

            finance.type = AllProductDataBean.productTypeFinance;
            allProductDataBeenList.add(finance);
        }

        for (AllProductDataBean fund : funds) {

            fund.type = AllProductDataBean.productTypeFund;
            allProductDataBeenList.add(fund);
        }

        for (AllProductDataBean invest : invests) {

            invest.type = AllProductDataBean.productTypeInvest;
            allProductDataBeenList.add(invest);
        }

        //数据打乱
        Collections.shuffle(allProductDataBeenList);

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(new AllProductListAdapter(mContext, allProductDataBeenList));

    }
}
